require(['modules/fetch'],function(fetch){
    
    //loadStoryList();
    var loadStoryListFn = function (){
        fetch('listStory', { ps: 20, pn: 0},
            function (result) {
                if(0 == result.errno ){
                    vueMainData.sidebarDataList = result.data.list;
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }

    function getStoryInfo (storyId){
        fetch('getStoryInfo', { story_id: storyId}, 
            function (result) {
                if(0 == result.errno ){
                    $(window).scrollTop(0);
                    vueMainData.storyInfo = result.data.storyInfo;
                    vueMainData.authorInfo = result.data.authorInfo;
                    vueMainData.htmlContent = markdown.toHTML(result.data.storyInfo.content);
                }
            },
            function(err) {
                console.log("ajax faild");
            }
        );
    }

    var storyId = PageData.story_id;
    getStoryInfo(storyId);

    //main
    var vueMainData = {
        htmlContent: '',
        storyInfo:{},
        authorInfo: {},
        sidebarDataList: [],
    };
    var vm = new Vue({
        el: "#vue-app",
        data: vueMainData,
        methods:{
            writeStoryChapter: function(storyId,nodeId){
                location.href="/chapter/editChapter?story_id=" + parseInt(storyId) + "&node_id=" + parseInt(nodeId);
                return false;
            },
            getNodeChaptersInfo: function(storyId,nodeId){
                getNodeChaptersInfo(storyId, nodeId);
            },
            sidebarItemClick: function(item,index){
                getStoryInfo(parseInt(item.story_id));
                // location.href="/story/readStory?story_id=" + parseInt(item.story_id);
                // console.log(index,item);
            }
        }
    });
    function getNodeChaptersInfo (storyId, nodeId){
        console.log(arguments);
        fetch('getNodeChaptersInfo',{  node_id: nodeId}, 
            function (result) {
                if(0 == result.errno ){
                    var data = result.data[0];

                    if(data && data.chapter_id){
                        location.href="/chapter/readChapter?chapter_id=" + parseInt(data.chapter_id) ;

                    } else {
                        if(PageData.userInfo.user_id &&PageData.userInfo.user_id == vueMainData.storyInfo.author_id ){
                            var go = confirm("要继续创作吗？");
                            if(go){
                                location.href="/chapter/editChapter?story_id=" + parseInt(storyId) + "&node_id=" + parseInt(nodeId);
                            }
                        } else {
                            alert("此章节还没有完成");
                        }
                        
                        
                        
                    }
                    return false;
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }
    loadStoryListFn();
});